#pragma once
#include "pch.h"
#include "DirectXView.h"

void DirectXView::Initialize(CoreApplicationView^ applicationView)
{
	applicationView->Activated += ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs^ > ( this, &DirectXView::OnActivated);

	Running = true;
}

void DirectXView::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
	CoreWindow::GetForCurrentThread()->Activate();
}

void DirectXView::Load(String^ entryPoint)
{

}

void DirectXView::Run()
{
	while (Running)
	{
		coreWindow->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);

		if (application != nullptr)
		{
			application->Render();
		}
		else
		{
			Running = false;
		}
	}
}

void DirectXView::SetWindow(CoreWindow^ window)
{
	coreWindow = CoreWindow::GetForCurrentThread();

	coreWindow->Closed += ref new TypedEventHandler<CoreWindow ^, CoreWindowEventArgs^ >(this, &DirectXView::OnCoreWindowClosed);
	coreWindow->Activated += ref new TypedEventHandler<CoreWindow ^, WindowActivatedEventArgs ^>(this, &DirectXView::OnCoreWindowActivate);

	application = ref new MyApplication();
}
	
void DirectXView::Uninitialize()
{
	application->Unload();
}

void DirectXView::OnCoreWindowClosed(CoreWindow^ window, CoreWindowEventArgs^ args)
{
	Running = false;
}

void DirectXView::OnCoreWindowActivate(CoreWindow ^ window, WindowActivatedEventArgs ^ args)
{
	if (args->WindowActivationState == CoreWindowActivationState::Deactivated)
	{
		Running = false;
	}
}